home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Experimental BBS Explossion 3
/
Experimental BBS Explossion III.iso
/
c
/
pcw.zip
/
MENUS.DOC
< prev
next >
Wrap
Text File
|
1991-12-29
|
19KB
|
431 lines
----+L---1----+----2----+----3----+----4----+----5----+----6----+----7----J----8
The PC Windows Menus
PC Windows includes functions for pop-up and a Lotus 1-2-3 type
menu. The discussion of the menus is seperate because they require
some explanation of their set-up. The menu functions were built to
take advantage of the way C treats arrays and pointers similarly. If
you are learning C for the first time the way the menus in PCW are
established may seem a bit arcane, but if you have patience and
study the examples you will learn a valuable lesson about pointers to
arrays of structures.
To begin, you will find the structure definitions for the PCW
menus in a file called MENU.H. When you browse the definitions do
not be alarmed or put off, we are going to explain them by way of
example for both kinds of menus. To start, we are going to create a
pop-up menu because it is the simpler of the two PCW menus and will
pave the way for the more complex stacked Lotus menu.
The Pop-Up Menu
If you have used PCs for any length of time you have undoubtedly
run across the venerable pop-up menu. They are everywhere in PC
applications, and with PCW you can now put them in your C
applications. The pop-up menus in PCW are window oriented which means
you can create the pop-up on the screen accept the input from it and
remove it from the video display when you are ready to. Since the
menu is in a window this implies window functions in PCW are used to
create and manage the window. This means when you make a call to
create the pop-up menu with the makepmenu() function that you will
have to supply information about the window the menu is to operate in.
For example, the window dimensions, the window colors, the border
colors, the title , and the title color. This is easily accomplished
with C structures. We simply initialize some structures with the
information about the window and pass pointers to these structures to
the menu functions where all of the work is done. For pop-up menus in
PCW we will be concerned with three structure definitions.
Building an Example
If you look in the MENU.H file you will notice the following
structure definition.
typedef struct {
char select_key;
char *item;
}PMNUFLDS;
This structure definition is used to define the pop-up menu
options and a value that uniquely identifies an option. For example
you may want to create menu with four options, 1. Delete, 2. Add, 3.
Change, and E. Exit. To do this with the PCW pop-up menu you would
simply create and initialize an array of type PMNUFLDS like this.
static PMNUFLDS poptions[] = {
{ '1', "1. Delete "},
{ '2', "2. Add "},
{ '3', "3. Change "},
{ 'E', "E. Exit "},
{ 0, (char *) 0}
};
The above example creates and initializes an array of structures of
type PMNUFLDS. This array will become the options used in the menu
later on. Notice the last line of the example and how the two fields
are intialized with NULL values. This is an old idea in C. It is
used to mark the end of the array since the array could be any size.
Now that we have the options of the menu defined we need to tell the
menu functions some information about the window it will use to show
the options and accept input from. Again, looking at the
structures in MENU.H you will notice the following structure
definition.
typedef struct {
MENU_WND pwnd;
int bar_pos;
PMNUFLDS *plist;
}PMNUTYPE;
We need to initialize a structure of PMNUTYPE with values and pass a
pointer to this structure to the pop-up menu functions. Looking at
the members of this structure we see that we have our PMNUFLDS list
with the array of menu options created above, and we can easily
declare the integer, but what is this MENU_WND stuff? Quite simply it
is another structure used to declare information about the window such
as its boundaries, color, border type and so on. Look at the
MENU.H file again and review the MENU_WND structure.
typedef struct menu_wnd {
WNDPTR *wnd; /* Window pointer */
int urow,ucol,lrow,lcol; /* Window coordinates */
int fcolor, bcolor; /* Window color */
int btype, bfclr,bbclr; /* Border type & color */
char *title; /* The title */
int tvloc, thloc, tfclr, tbclr; /* Title pos & color */
int cfclr, cbclr; /* Select bar color */
}MENU_WND;
Now to finish this example lets declare and initialize the PMNUTYPE
structure to our program.
PMNUTYPE pmenu = {
NULL, /* Initial Window Pointer */
13, 20, 20, 60, /* Window boundaries */
BLACK, LIGHTGRAY, /* Window Color */
DOUBLEALL,RED,LIGHTGRAY, /* Border type and border color */
" Main Pop-up Menu ", /* Menu title */
TOP,MIDDLE,BLUE,LIGHTGRAY, /* Title location & color */
WHITE,BLUE, /* Select Bar color */
0, /* Initial bar position */
poptions /* Pointer to array of options */
}
Whew! That's a lot of information, but considering all of the work
PCW is going to do with this information your work is worth the
effort. Now that we have everything declared we can create the menu
and then accept input from it using two function makepmenu() and
pmenuinput(). Lets explain the operation of these two functions. The
makepmenu() function will accept a pointer value to data type PMNUTYPE
and create the window, and the menu and return to your program a
pointer of type WNDPTR (a handle to a window). If the Mouse driver is
present and has been initialized with init_mouse() makepmenu() will
turn on the mouse for use in menu input. The pmenuinput() function
will, again, accept a pointer to type PMNUTYPE and accept input from
the keyboard. It will interpret the arrow keys, tab keys, mouse, and
the enter key. Futhermore, the select characters you defined in the
PMNUFLDS structure are also active when pmenuinput() is invoked. For
example if you pressed '1' at the keyboard pmenuinput() would
highlight the Delete option and return the '1' character to your
program where you could do with it as you please. Needless to say,
you should call makepmenu() before you call pmenuinput().
A Complete Example
Finally putting it all together we can give a complete working
example.
#include <stdio.h>
#include <pcwproto.h>
#include <menu.h>
/* Declare the menu options */
static PMNUFLDS poptions[] = {
{ '1', "1. Delete "},
{ '2', "2. Add "},
{ '3', "3. Change "},
{ 'E', "E. Exit "},
{ 0, (char *) 0}
};
/* Declare and initialize window info */
PMNUTYPE pmenu = {
NULL, /* Initial Window Pointer */
9, 25, 14, 55, /* Window boundaries */
BLACK, LIGHTGRAY, /* Window Color */
DOUBLEALL,RED,LIGHTGRAY, /* Border type and border color */
" Main Pop-up Menu ", /* Menu title */
TOP,MIDDLE,BLUE,LIGHTGRAY, /* Title location & color */
WHITE,BLUE, /* Select Bar color */
0, /* Initial bar position */
poptions /* Pointer to array of options */
};
void main(void) {
int i = 0;
WNDPTR *menuwnd;
static char *selections[] = {
"Delete ",
"Add ",
"Change "
};
init_mouse(); /* Initialize Mouse */
menuwnd = makepmenu(&pmenu); /* Create Pop-up menu */
while (i != 'E' && i != 27) { /* Do until exit or ESC */
i = pmenuinput(&pmenu); /* Get return value from menu */
switch(i) { /* Make a decision */
case '1' :
case '2' :
case '3' :
i -= 49;
qputs(25,CENTER,WHITE,BLACK,selections[i]);
break;
default : break;
}
}
if (mpresent) hide_mouse(); /* Hide mouse if home */
menuwnd = wpop(menuwnd); /* Remove the menu window */
}
That was easy! Just a couple of more things about the menu functions.
When accepting input from a menu the ESC key will always return to
your program with a value of 27 (the ESC key value) where you can do
with it what you will. In the above example it was used to exit the
loop and terminate the program. Also the right Mouse key is treated
as though it were an ESC key, and BOTH Mouse keys are treated as the
enter key, and finally the LEFT Mouse key is used to select a
particular item from the menu.
The Picklist Menu
The Picklist Menu is very similar to the Pop-Up Menu with one minor
exception: it does not require a list of type PMNUFLDS. It does
require, however, an array of pointers to character strings which act
as the options. And instead of returning a selection key the Picklist
menu returns a subscript value to the character string which was
chosen. Again, we will explain by way of a working example.
If you again look into the MENU.H file you will find a structure
declaration like the one below:
typedef struct pick_list {
MENU_WND twnd;
char **list;
int bar_pos, offset;
}PICKLIST;
As before, the MENU_WND member of this structure is another structure
that will be used to contain information about the size, location, and
color of the window that will be used for this menu. The **list
member field is a pointer to an array of pointers to character
strings. These strings will be the actual options for this menu. The
bar_pos field is initialized to the beginning bar position in the
menu, and the offset value is initialized to the first option in the
list. Usually the bar_pos and offset values are intialized to zero
when the menu is first created. Now a working examples of the
Picklist menu:
#include <stdio.h>
#include "pcwproto.h"
#include "menu.h"
static char *selections[] = {
"Methodology",
"Word Processors",
"C Compiler",
"Pascal Compiler",
"3270 Emulation",
"Communications",
"COBOL Compiler",
"MASM",
NULL
};
static PICKLIST plmenu = {
NULL,
15, 30, 19, 50, BLACK,LIGHTGRAY, /* Window Parms */
2, RED,LIGHTGRAY, /* Border Parms */
" Main Menu ",TOP,MIDDLE,BLUE,LIGHTGRAY, /* Title Parms */
WHITE,BLUE, /* Select Bar color */
selections, /* pointer to menu list */
0,0 /* bar position offset */
};
int main ( void ) {
int i = 0;
int mxr, mxc;
WNDPTR *wnd;
chk_video_state(&mxr,&mxc); /* Get max rows & cols */
init_mouse(); /* turn on mouse if there */
wnd = make_pick_list(&plmenu); /* Make a popup window */
while (i != -1) { /* Do until Esc */
i = get_pick_list(&plmenu); /* Get Menu return value */
if ( i == -1 ) continue; /* ESC was pressed */
if ( mpresent ) hide_mouse(); /* Hide the mouse */
qhchar(22,1,BLACK,BLACK,32,80); /* Clear msg line */
qputs(22,99,RED,7,selections[i]); /* Write the choice */
if (mpresent) show_mouse();
}
if (mpresent) hide_mouse(); /* Hide the mouse */
wnd = wpop(wnd); /* Get rid of menu */
return 0; /* And go home */
}
The Lotus Menu
Setting up the Lotus menu is very similar to setting up the pop-up
menu. The only thing that is different about the Lotus menu is that
more than one menu can be stacked into a window at one time, and this
requires some special setting up. Looking at the structures in MENU.H
you will see the following definition.
typedef struct {
char select_key;
int select_col;
char *item;
char *item_msg;
}LMNUFLDS;
This structure is similar to the PMNUFLDS structure in that it
contains the select_key member and the *item member, but two new
members have been added. The select_col member is used to specify
which column in the window you want this option to begin. The
*item_msg member is a short description of what this option does.
Now creating the options list for the menu is almost the same as for
the pop-up menu. Just look.
LMNUFLDS menu1[] = {
{ 'A', 2, "Add", "Add an Entry to Database"},
{ 'C', 7, "Change", "Change a Database Entry"},
{ 'D', 15,"Delete", "Delete a Database Entry"},
{ NULL, NULL, NULL, NULL}
};
LMNUFLDS menu2[] = {
{ 'P', 2, "Purge", "Purge the Workfile"},
{ 'S', 10,"Send", "Send Electronic Mail"},
{ 'E', 15,"Exit", "Exit the Program"},
{ NULL,NULL,NULL,NULL }
};
LMNUFLDS *menulist[] = {menu1, menu2, NULL};
Let's take a look at what we have done here. First we have created an
array of data structure type LMNUFLDS and have called it menu1[].
Notice that it is NULL terminated. Now since more than one menu can
be stacked into one window we create the second menu and call it
menu2[]. Now, how do we tie all these menus together? Simple, we
create an array of pointers of type LMNUFLDS and initialize it with
the beginning addresses of each of the menus and make sure that it too
is NULL terminated. If you wanted more menus in one window then you
would just define the menu options just as we did for menu1[] and
menu[]2 and assign its beginning address to an array of pointers.
This way you can stack as many menus into one window as you like.
Now to complete this example we must declare and initialize a
structure defined as LMNUTYPE. Looking at MENU.H we see it is
declared as follows.
typedef struct {
MENU_WND lwnd;
int bar_pos, wnd_pos;
LMNUFLDS **llist;
}
Let's explain a little bit here. First MENU_WND you have already been
introduced to. It was used for the Pop-up menu too. The two members
bar_pos and wnd_pos will be initialized to a value to indicate which
menu to start with and which option the selection bar will be
positioned on. Finally, the **llist member is a pointer to a pointer
and will be initialized with the beginning address of our array of
pointers to arrays of type LMNUFLDS. Whew, got that? Okay, the
complete example.
#include <stdio.h>
#include <pcwproto.h>
#include <menu.h>
static LMNUFLDS menu1[] = {
{ 'A', 2, "Add", "Add an Entry to Database"},
{ 'C', 17,"Change", "Change a Database Entry"},
{ 'D', 33,"Delete", "Delete a Database Entry"},
{ NULL, NULL, NULL, NULL}
};
static LMNUFLDS menu2[] = {
{ 'P', 2, "Purge", "Purge the Workfile"},
{ 'S', 18,"Send", "Send Electronic Mail"},
{ 'E', 35,"Exit", "Exit the Program"},
{ NULL,NULL,NULL,NULL }
};
static LMNUFLDS *menulist[] = {menu1, menu2, NULL};
static LMNUTYPE lmenu = {
NULL,
10, 20, 13, 60,
BLUE, LIGHTGRAY,
DOUBLEALL,RED,LIGHTGRAY,
" The Lotus ",
TOP,MIDDLE,BLACK,LIGHTGRAY,
WHITE,BLUE,
0,0,menulist
};
static char *selections[] = {
"Add ", "Change ",
"Delete ", "Purge ",
"Send "
};
void main(void) {
int i = 0;
int index = 0;
WNDPTR *menuwnd;
init_mouse();
menuwnd = makelmenu(&lmenu);
while (i != 'E' && i != 27) {
i = lmenuinput(&lmenu);
switch(i) {
case 'A' : index = 0; break;
case 'C' : index = 1; break;
case 'D' : index = 2; break;
case 'P' : index = 3; break;
case 'S' : index = 4; break;
case 27 :
case 'E' : continue;
}
qputs(25,CENTER,WHITE,BLACK,selections[index]);
}
if (mpresent) hide_mouse();
menuwnd = wpop(menuwnd);
}
Well, there they are. Explanations and examples. I know this seems
to be a lot of work for a couple of menus, but they can significantly
enhance the way your programs look and feel to the user not to mention
making you look like a genius in the process. After a little work the
menus almost seem effortless. So have fun and enjoy and make some
great looking screens and menus with PC windows.